package de.gwtprime.client.widgets;

import java.util.List;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;

import de.gwtprime.client.ClientFactory;
import de.gwtprime.client.PrimeRequestFactory;
import de.gwtprime.client.event.ApplyTabHeightEvent;
import de.gwtprime.client.event.QuestionAddedEvent;
import de.gwtprime.client.event.handler.QuestionAddedEventHandler;
import de.gwtprime.client.user.ACLManager.PrimeUIElement;
import de.gwtprime.shared.requestFactory.ArticleSectionProxy;
import de.gwtprime.shared.requestFactory.QuestionProxy;
import de.gwtprime.shared.requestFactory.request.ArticleSectionRequest;

public class QuestionTabWidget extends Composite{

	interface QuesitonTabWidgetUiBinder extends UiBinder<Widget, QuestionTabWidget>{}
	
	QuesitonTabWidgetUiBinder uiBinder = GWT.create(QuesitonTabWidgetUiBinder.class);
	
	ClientFactory clientFactory;
	ArticleSectionProxy articleSection;
	
	@UiField FlowPanel questionListPanel;
	
	@UiField TooltipButton addQuestionButton;
	
	@UiField SimplePanel addQuestionPanel;
	@UiField SimplePanel questionPreviewWidget;
	
	public QuestionTabWidget(final ClientFactory clientFactory, final ArticleSectionProxy articleSection) {
		initWidget(uiBinder.createAndBindUi(this));
		
		this.clientFactory = clientFactory;
		this.articleSection = articleSection;
		
		
		addStyleName("questionsTabWidget");
		addQuestionButton.enableFromUser(clientFactory, PrimeUIElement.QuestionEditBtn);
		
		addQuestionButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				if(!addQuestionButton.checkEnabledStateAndShowMessage(event)){
					return;
				}
				
				addQuestionPanel.add(new AddQuestionWidget(articleSection, clientFactory));
				questionPreviewWidget.add(new QuestionWidget(null, clientFactory));
				addQuestionButton.setVisible(false);
				clientFactory.getEventBus().fireEvent(new ApplyTabHeightEvent());
			}
		});
		
		final PrimeRequestFactory requestFactory = clientFactory.getRequestFactory();

		ArticleSectionRequest request = requestFactory.articleSectionRequest();
		Request<List<QuestionProxy>> createReq = request.loadQuestions().using(articleSection).with("answers", "user");
		createReq.fire(new Receiver<List<QuestionProxy>>() {
			@Override
			public void onSuccess(List<QuestionProxy> questions) {
				for(QuestionProxy question: questions){
					questionListPanel.add(new QuestionPreviewWidget(question, clientFactory));
				}
			}
			
		});
		
		
		clientFactory.getEventBus().addHandler(QuestionAddedEvent.TYPE, new QuestionAddedEventHandler() {
			
			@Override
			public void onQuestionAdded(QuestionAddedEvent event) {
				
				questionListPanel.add(new QuestionPreviewWidget(event.getQuestion(), clientFactory));
				clientFactory.getEventBus().fireEvent(new ApplyTabHeightEvent());
				
			}
		});
		
	}
}
